class ProductCategory {
    String name
    ProductCategory parent
    static hasMany = [children:ProductCategory, products:Product]
    static belongsTo = ProductCategory
    static transients = ["sortedChildren", "sortedProducts"]

    void addChild(ProductCategory child){
        child.parent = this
        this.addToChildren(child)
    }

    static constraints = {
        parent(nullable:true)
    }

    def getSortedChildren(){
        final def unsorted = children.collect {it}
        return unsorted.sort{ProductCategory e1,ProductCategory e2->
            return e1.name.compareTo(e2.name)

        }
    }

    def getSortedProducts(){
        final def unsorted = products.collect {it}
        return unsorted.sort{Product e1,Product e2->
            return e1.name.compareTo(e2.name)
        }
    }

    String toString(){
        return parent?"$parent->$name":"$name"
    }
}
